//
//  BaseTableViewController.swift
//  Weibo
//
//  Created by 闫博123 on 16/1/11.
//  Copyright © 2016年 闫博123. All rights reserved.
//

/*
编写代码的顺序
    // MARK: 外部访问属性 & 重要变化属性
    // MARK: 外部访问的方法
    // MARK: 监听方法
    // MARK: 设置界面
    // MARK: 内部属性 & 懒加载控件
*/


import UIKit

class BaseTableViewController: UITableViewController , visitorLoginViewDelegate {
    
    // 定时用户是否登录标识
    var userLogin = UserAccount.loadUserAccount() != nil
    
    // 访客视图
    var visitorloginView : VisitorLoginView?
    
    // loadView() 苹果专门为 手写代码准备的方发 一旦实现该方法 SB 和 xib 自动失效
    // 准备视图层次结构 在UIViewController中会将View准备出来
    override func loadView()
    {
        // 自定义view
        // 如果用户登录 就调用父类的loadView view就为tableView
        // 如果没有登录 就不调用父类的loadView 自定义访客视图 view就为自定义的访客视图
        if userLogin
        {
            super.loadView()
        }
        else
        {
            // 自定义访客视图
            visitorloginView = VisitorLoginView()
            
            // 设置代理
            visitorloginView?.visitorLoginDelegate = self
            
            view = visitorloginView
            
            // 设置访客视图导航条的item
            self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "注册", style: .Plain, target: self, action: "userWillRegister")
            
            self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "登陆", style: .Plain, target: self, action: "userWillLogin")
        }
    }
    
    // MARK: 实现visitorLoginViewDelegate代理方法
    func userWillLogin()
    {
        let oauth = OAuthViewController()
        
        let nav = UINavigationController(rootViewController: oauth)
        
        // 跳转到授权页面
        presentViewController(nav, animated: true, completion: nil)
        
        
    }
    
    func userWillRegister()
    {
        
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 0
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return 0
    }

    /*
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath)

        // Configure the cell...

        return cell
    }
    */

    /*
    // Override to support conditional editing of the table view.
    override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        // Return false if you do not want the specified item to be editable.
        return true
    }
    */

    /*
    // Override to support editing the table view.
    override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        if editingStyle == .Delete {
            // Delete the row from the data source
            tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
        } else if editingStyle == .Insert {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }    
    }
    */

    /*
    // Override to support rearranging the table view.
    override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {

    }
    */

    /*
    // Override to support conditional rearranging of the table view.
    override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        // Return false if you do not want the item to be re-orderable.
        return true
    }
    */

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
